#ifndef P2PPEER_H
#define P2PPEER_H

#include <QObject>
#include <QMultiMap>
#include <QHostAddress>
class   QUdpSocket;
class   QTimer;
class   ThreadPeer;

class P2PPeer : public QObject
{
    Q_OBJECT
public:
    explicit P2PPeer(QObject *parent = 0);
    //在childSocketList中增加一个item
    void    addOneChild(QString childSocket,qint64 macAddr);
    void    removeOneChild(QString childSocket,qint64 macAddr);
    void    distriVideo(QByteArray &data,qint64 macAddr);

    void    notifyPeerDisconnected(QHostAddress  iP,quint16 port);
    //将udpSocketForDistri指向udpSocket
    void    setUdpSocket(QUdpSocket*  udpSocket);

    //当某个子节点socket发来心跳包时，调用下面的函数，将TTL置为最大值P2P_TTL_MAX
    QByteArray    updateChildSocketTTLMap(QString socket);
    void testThread(); //用于调试多线程

    /*
addrSocketMap用于保存mac地址和childSocket的对应关系。
      某个childSocket的例子“10.21.14.15:5864”.
由于某个socket可能同时观看多个视频，所以是一对多的关系
            */
    QMultiMap <QString,qint64> addrChildSocketMap;//mac地址和socket的映射
     void quitThreadPeers(); //退出这些线程

private:

    QList <ThreadPeer*> listThreadPeer;

    QTimer  *timerForTTL; //用于处理心跳的时钟
    QByteArray dataToBeSent;

    /*
     childSocketTTLMap用于记录socket的活动状态。
     */
    QMap    <QString,qint8>   childSocketTTLMap;
    QUdpSocket  *udpSocketForDistri;

signals:
   void sigSendData(QByteArray data,QString destAddr,
        quint16 destPort,quint8 paraID);

public slots:
    void timerForTTLEvent();
    void connectThread();

};

#endif // P2PPEER_H
